<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>其他相关方法 | vue2-element-dict</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/vue2-element-dict/favicon.ico">
    <script defer="defer" async="async" src="https://cpro.baidustatic.com/cpro/ui/cm.js"></script>
    <meta name="description" content="vue2-element-dict字典包官方使用文档">
    
    <link rel="preload" href="/vue2-element-dict/assets/css/0.styles.4793a278.css" as="style"><link rel="preload" href="/vue2-element-dict/assets/js/app.b15cbcb6.js" as="script"><link rel="preload" href="/vue2-element-dict/assets/js/4.26f72541.js" as="script"><link rel="preload" href="/vue2-element-dict/assets/js/32.1fb6dad3.js" as="script"><link rel="prefetch" href="/vue2-element-dict/assets/js/1.de614e96.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/10.2f2bf95a.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/11.ff41966b.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/12.b8277606.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/13.b04a73f4.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/14.4d6242d1.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/15.630dac4e.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/16.a7c1d843.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/17.2b8a9633.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/18.8eb3630c.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/19.6786c2bb.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/2.53443f2d.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/20.a5157105.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/21.d4d5c2c2.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/22.8b3228db.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/23.c5c8d87f.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/24.fccc0622.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/25.01db42e3.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/26.faa5aea5.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/27.80a1c84f.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/28.a9a8221b.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/29.2fcd2f4e.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/30.dd694ba1.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/31.99cbdf15.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/33.7b640408.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/34.1c4b9978.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/35.a2561dd0.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/36.c02c0325.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/37.f5704977.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/5.0ef459b8.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/6.d8b0c3f6.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/7.08430ef6.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/8.d5c78b56.js"><link rel="prefetch" href="/vue2-element-dict/assets/js/9.cbf8f2c2.js">
    <link rel="stylesheet" href="/vue2-element-dict/assets/css/0.styles.4793a278.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vue2-element-dict/" class="home-link router-link-active"><!----> <span class="site-name">vue2-element-dict</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/vue2-element-dict/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/vue2-element-dict/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="https://www.npmjs.com/package/vue2-element-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  npm地址
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/vue2-element-dict/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/vue2-element-dict/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="https://www.npmjs.com/package/vue2-element-dict" target="_blank" rel="noopener noreferrer" class="nav-link external">
  npm地址
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>捐赠</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>指南</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue2-element-dict/guide/" class="sidebar-link">介绍</a></li><li><a href="/vue2-element-dict/guide/fast/" class="sidebar-link">快速开始</a></li><li><a href="/vue2-element-dict/guide/directory-structure/" class="sidebar-link">目录结构</a></li><li><a href="/vue2-element-dict/guide/config/" class="sidebar-link">字典包配置</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>普通字典组件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue2-element-dict/components/el-select-dict/" class="sidebar-link">select-dict选择器</a></li><li><a href="/vue2-element-dict/components/el-radio-dict/" class="sidebar-link">radio-dict单选框</a></li><li><a href="/vue2-element-dict/components/el-checkbox-dict/" class="sidebar-link">checkbox-dict多选框</a></li><li><a href="/vue2-element-dict/components/el-radio-button-dict/" class="sidebar-link">radio-button-dict单选框</a></li><li><a href="/vue2-element-dict/components/el-checkbox-button-dict/" class="sidebar-link">checkbox-button-dict多选框</a></li><li><a href="/vue2-element-dict/components/el-tabs-dict/" class="sidebar-link">tabs-dict标签页</a></li><li><a href="/vue2-element-dict/components/el-tag-dict/" class="sidebar-link">tag-dict标签</a></li><li><a href="/vue2-element-dict/components/el-link-dict/" class="sidebar-link">link-dict链接</a></li><li><a href="/vue2-element-dict/components/el-text-dict/" class="sidebar-link">text-dict链接</a></li><li><a href="/vue2-element-dict/components/el-button-dict/" class="sidebar-link">button-dict链接</a></li><li><a href="/vue2-element-dict/components/el-table-colmun-dict/" class="sidebar-link">el-table-colmun-dict表格</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>树形字典组件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue2-element-dict/components/el-cascader-dict/" class="sidebar-link">cascader-dict联级选项</a></li><li><a href="/vue2-element-dict/components/el-tree-dict/" class="sidebar-link">tree-dict树菜单</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>过滤器</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue2-element-dict/filters/dict/" class="sidebar-link">字典过滤器</a></li><li><a href="/vue2-element-dict/filters/tree-dict/" class="sidebar-link">树形字典过滤器</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>方法</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue2-element-dict/functions/dict/" class="sidebar-link">字典方法</a></li><li><a href="/vue2-element-dict/functions/other/" aria-current="page" class="active sidebar-link">其他方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/vue2-element-dict/functions/other/#使用方式" class="sidebar-link">使用方式</a></li><li class="sidebar-sub-header"><a href="/vue2-element-dict/functions/other/#日期格式" class="sidebar-link">日期格式</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="其他相关方法"><a href="#其他相关方法" class="header-anchor">#</a> 其他相关方法</h1> <p>此处为本字典包暴露的其他与字典无关的相关方法，均为函数调用方式，具体方法如下</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token punctuation">{</span> options <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;less&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * @description: 获取字典包配置数据
 * @return {dictConfig 字典包配置数据  配置与默认配置的合并}
 * @author: syx
 */</span>
<span class="token function">getDictConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * @description: 通过字段获取dictConfig的配置
 * @param key dictConfig的字段
 * @return {dictConfig的某个字段}
 * @author: syx
 */</span>
<span class="token function">getDictConfigByKey</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 判断某个字符串是不是日期格式
 * @param str 字符串
 * @return {boolean}
 * @author: syx
 */</span>
<span class="token function">isDate</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 将某个日期格式化
 * @param date 日期 可以是字符串 数字 date()
 * @param format  默认datetime 可选  date month time year  也可自定义格式，具体配置继续阅读
 * @return 根据format返回的日期格式
 * @author: syx
 */</span>
<span class="token function">formatDate</span><span class="token punctuation">(</span>date<span class="token punctuation">,</span> format<span class="token punctuation">,</span> defaultVal<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 将某个字符串进行脱敏
 * @param str 字符串
 * @param options { 配置
 *   start: 1,  展示前面几位数 默认1
 *   end: 0, 展示后面几位数 默认0
 *   symbol: &quot;*&quot; 使用什么符号进行脱敏  默认 *
 *   middle  如果有配置，则表示 中间展示几个symbol
 * } 
 * @return 根据format返回的日期格式
 * @author: syx
 */</span>
<span class="token function">mask</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span> options<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 姓名脱敏
 * @param name 姓名
 * @param symbol &quot;*&quot; 使用什么符号进行脱敏  默认 *
 * @return {*}
 * @author: syx
 */</span>
<span class="token function">maskName</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> symbol<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 身份证号脱敏
 * @param idCard 身份证号码
 * @param symbol &quot;*&quot; 使用什么符号进行脱敏  默认 *
 * @return {*}
 * @author: syx
 */</span>
<span class="token function">maskIdCard</span><span class="token punctuation">(</span>idCard<span class="token punctuation">,</span> symbol<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 手机号脱敏
 * @param phone 手机号码
 * @param symbol &quot;*&quot; 使用什么符号进行脱敏  默认 *
 * @return {*}
 * @author: syx
 */</span>
<span class="token function">maskPhone</span><span class="token punctuation">(</span>phone<span class="token punctuation">,</span> symbol<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 地址脱敏
 * @param address 地址
 * @param symbol &quot;*&quot; 使用什么符号进行脱敏  默认 *
 * @return {*}
 * @author: syx
 */</span>
<span class="token function">maskAddress</span><span class="token punctuation">(</span>address<span class="token punctuation">,</span> symbol<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 脱敏字符串
 * @param str 要脱敏的数据
 * @param options { 配置
 *   start: 1,  展示前面几位数 默认1
 *   end: 0, 展示后面几位数 默认0
 *   symbol: &quot;*&quot; 使用什么符号进行脱敏  默认 *
 *   middle  如果有配置，则表示 中间展示几个symbol
 *   defaultVal: &quot;&quot;, 没值时返回的默认值
 *   maskType: &quot;&quot; 脱敏类型   mobile，name，address，idCard
 * } 
 * @return {*}
 * @author: syx
 */</span>
<span class="token function">desensitization</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span> options<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 一维数组转化为 树形数组
 * @param data 一维数组
 * @param config { 配置项
 *    idField: &quot;id&quot;, //唯一标识字段名
 *    parentIdField: &quot;parentId&quot;, //父节点唯一标识字段名
 *    childrenField: &quot;children&quot;, //子节点标识字段名
 *    firstId: &quot;0&quot;, // 根节点值
 *    labelField: &quot;label&quot;, //label字段名
 *    labelArrField: &quot;labelArr&quot;, //给对象新增的中文数组字段名
 *    idArrField: &quot;idArr&quot;, //给对象新增的id数组字段名
 *    levelField: &quot;level&quot;, //给对象新增的层级字段名
 *    level: 0, // 给根目录配置的层级
 *    leafField: &quot;leaf&quot; //叶子节点标识字段名
 * }
 * @return {*}
 * @author: syx
 */</span>
<span class="token function">listToTree</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> config<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 通过code 获取树形数据item的数组
 * @param code 值
 * @param arr 一维数组
 * @param options { // 配置项
 *   idField: &quot;id&quot;, //唯一标识字段名
 *   parentIdField: &quot;parentId&quot; //父节点唯一标识字段名
 * }
 * @return {*}
 * @author: syx
 */</span>
<span class="token function">getTreeItemByCode</span><span class="token punctuation">(</span>code<span class="token punctuation">,</span> arr<span class="token punctuation">,</span> options<span class="token punctuation">)</span>

<span class="token comment">/**
 * @description: 通过label 获取树形数据item的数组
 * @param label 值
 * @param arr 一维数组
 * @param options { // 配置项
 *   idField: &quot;id&quot;, //唯一标识字段名
 *   labelField: &quot;label&quot;, //唯一标识字段名
 *   parentIdField: &quot;parentId&quot; //父节点唯一标识字段名
 * }
 * @return {*}
 * @author: syx
 */</span>
<span class="token function">getTreeItemByLabel</span><span class="token punctuation">(</span>label<span class="token punctuation">,</span> arr<span class="token punctuation">,</span> options<span class="token punctuation">)</span>
</code></pre></div><h2 id="使用方式"><a href="#使用方式" class="header-anchor">#</a> 使用方式</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span>getDictConfig<span class="token punctuation">,</span> getDictConfigByKey<span class="token punctuation">,</span> isDate<span class="token punctuation">,</span> formatDate<span class="token punctuation">,</span> mask<span class="token punctuation">,</span> maskName<span class="token punctuation">,</span> maskIdCard<span class="token punctuation">,</span> maskPhone<span class="token punctuation">,</span> maskAddress<span class="token punctuation">,</span> desensitization<span class="token punctuation">,</span> listToTree<span class="token punctuation">,</span> getTreeItemByCode<span class="token punctuation">,</span> getTreeItemByLabel<span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;vue2-element-dict&quot;</span>
</code></pre></div><h2 id="日期格式"><a href="#日期格式" class="header-anchor">#</a> 日期格式</h2> <table><thead><tr><th style="text-align:left;">标识</th> <th style="text-align:left;">示例</th> <th style="text-align:left;">描述</th></tr></thead> <tbody><tr><td style="text-align:left;">yy</td> <td style="text-align:left;">18</td> <td style="text-align:left;">年，两位数</td></tr> <tr><td style="text-align:left;">yyyy</td> <td style="text-align:left;">2018</td> <td style="text-align:left;">年，四位数</td></tr> <tr><td style="text-align:left;">M</td> <td style="text-align:left;">1-12</td> <td style="text-align:left;">月，从1开始</td></tr> <tr><td style="text-align:left;">MM</td> <td style="text-align:left;">01-12</td> <td style="text-align:left;">月，两位数字</td></tr> <tr><td style="text-align:left;">d</td> <td style="text-align:left;">1-31</td> <td style="text-align:left;">日</td></tr> <tr><td style="text-align:left;">dd</td> <td style="text-align:left;">01-31</td> <td style="text-align:left;">日，两位数</td></tr> <tr><td style="text-align:left;">h</td> <td style="text-align:left;">0-23</td> <td style="text-align:left;">24小时</td></tr> <tr><td style="text-align:left;">hh</td> <td style="text-align:left;">00-23</td> <td style="text-align:left;">24小时，两位数</td></tr> <tr><td style="text-align:left;">h</td> <td style="text-align:left;">1-12</td> <td style="text-align:left;">12小时</td></tr> <tr><td style="text-align:left;">hh</td> <td style="text-align:left;">01-12</td> <td style="text-align:left;">12小时，两位数</td></tr> <tr><td style="text-align:left;">m</td> <td style="text-align:left;">0-59</td> <td style="text-align:left;">分钟</td></tr> <tr><td style="text-align:left;">mm</td> <td style="text-align:left;">00-59</td> <td style="text-align:left;">分钟，两位数</td></tr> <tr><td style="text-align:left;">s</td> <td style="text-align:left;">0-59</td> <td style="text-align:left;">秒</td></tr> <tr><td style="text-align:left;">ss</td> <td style="text-align:left;">00-59</td> <td style="text-align:left;">秒，两位数</td></tr></tbody></table></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">5/17/2023, 10:19:54 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/vue2-element-dict/functions/dict/" class="prev">
        字典方法
      </a></span> <!----></p></div> </main></div><div class="global-ui"><div id="loading-mask"><div class="loading-wrapper"><span class="loading-dot loading-dot-spin"><i></i> <i></i> <i></i> <i></i></span></div></div><!----></div></div>
    <script src="/vue2-element-dict/assets/js/app.b15cbcb6.js" defer></script><script src="/vue2-element-dict/assets/js/4.26f72541.js" defer></script><script src="/vue2-element-dict/assets/js/32.1fb6dad3.js" defer></script>
  </body>
</html>
